package com.hfzy.ihk.web.websocket.support.toBrowser;

import com.hfzy.ihk.common.util.http.WebUtil;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.web.websocket.conf.PublicConfig;
import com.hfzy.ihk.web.websocket.constant.Constants;
import com.hfzy.ihk.web.websocket.support.base.CommonWebSocketHandshakeInterceptor;
import com.hfzy.ihk.web.websocket.support.WebSocketManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/20.
 *
 * @author wws
 */

public class BrowserWebSocketHandshakeInterceptor extends CommonWebSocketHandshakeInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(BrowserWebSocketHandshakeInterceptor.class);

    private String profiles;

    public  BrowserWebSocketHandshakeInterceptor(){
        super();
    }

    public  BrowserWebSocketHandshakeInterceptor(String profiles){
        super();
        this.profiles=profiles;
    }
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {

        /**连接安全验证*/
        boolean validateSource = super.beforeHandshake(request,response,wsHandler,attributes);

        if(!validateSource)
            return false;
        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        String userId = servletRequest.getParameter("userId");
            String token  = servletRequest.getParameter("token");

        logger.info("browser beforeHandshake ----》userId:{},token:{}",userId,token);

        if(StringUtil.isEmpty(userId) || StringUtil.isEmpty(token))
            return false;

         /*---------防止测试时，与前线的帐号冲突----------*/
        String ip = WebUtil.getIpAddr(((ServletServerHttpRequest) request).getServletRequest());

        boolean isTest;

        if(Constants.SYSTEM_ENV_PRO.equals(profiles))
            isTest = PublicConfig.CLOUDCALL_V2_TEST_IP_PRO.equals(ip);
        else
            isTest = PublicConfig.CLOUDCALL_V2_TEST_IP_DEV.contains(ip);

        if(isTest){
            attributes.put("isTest", Constants.TRUE);
            attributes.put("userId", WebSocketManager.createTestUserId(userId));
        }else {
            attributes.put("userId",userId);
        }

        attributes.put("token",token);

        int index_ = token.indexOf("_");
        if(index_ != -1) {
            attributes.put("machineId", token.substring(0, index_));
        }

        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        logger.debug("websocket after Handshake!");
    }

}
